package app.auto.runner.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import app.auto.runner.base.framework.CrashHandler;

/**
 * Created by hp on 2018/5/11.
 * String cat
 */
public class Prt {
    public static class Result {
        public String contentReplaced;
        public Collection<String> toreplaceList = new ArrayList();

    }

    public static Prt.Result getLinks(String html_content, String tagstart, String tagend, String linkstart) {
        return getLinks(html_content, tagstart, tagend, linkstart, false);
    }

    public static Prt.Result getLinks(String html_content, String tagstart, String tagend, String linkstart, boolean appendstart) {
        List<String> list = new ArrayList<String>();
        Prt prt = Prt.cat(tagstart, tagend, html_content);
        if (prt == null) {
            return new Prt.Result();
        }
        if (prt.getPrt().startsWith(linkstart) && !list.contains(prt.getPrt())) {
            list.add((appendstart ? tagstart : "") + prt.getPrt());
        }
        String part = html_content;

        while (part.contains(tagstart)) {
            try {
                prt = Prt.cat(tagstart, tagend, 0, part);
                if (prt == null || prt.getPrt() == null) {
                    break;
                }

                part = part.substring(prt.getBoundafteridx());
                if (prt.getPrt().startsWith(linkstart) && !list.contains(prt.getPrt())) {
                    list.add((appendstart ? tagstart : "") + prt.getPrt());
                }
            } catch (Exception e) {
//                            CrashHandler.errorDialog(e);
            }

        }

        Prt.Result result = new Prt.Result();
        result.contentReplaced = part;
        result.toreplaceList = list;
        return result;
    }

    private String prt;

    public Prt(String Prt) {
        this.prt = Prt;
    }

    private static Prt hold;

    public static Prt newInstance(String Prt) {
        return hold = new Prt(Prt);
    }

    public static Prt hold() {
        return hold;
    }

    public int getLeng() {
        return prt.length();
    }

    public int getAfterIndex(String whole, int fromindex) {
        try {
            return whole.indexOf(prt, fromindex) + getLeng() - 1;
        } catch (Exception e) {
            return -1;
        }
    }

    public int getAfter_startIndex(String whole, int fromindex) {
        try {
            return whole.indexOf(prt, fromindex);
        } catch (Exception e) {
            return -1;
        }
    }

    public int getAfterIndex(String whole) {
        return whole.indexOf(prt, 0) + getLeng();
    }

    public int getPreviousIndex(String whole, int fromindex) {
        return whole.indexOf(prt, fromindex);
    }

    public int getPreviousIndex(String whole) {
        return whole.indexOf(prt, 0);
    }

    public static Prt cat(String pre, String after, String whole) {
        return cat(pre, after, 0, whole);
    }

    public static String catInner(String pre, String after, int fromidx, String whole) {
        Prt afterprt = null;
        String p = null;
        int start = 0;
        try {
            start = new Prt(pre).getAfter_startIndex(whole, fromidx);
            if (start < 0) {
                return null;
            }

            p = whole.substring(start + pre.length(), (afterprt = new Prt(after)).getAfter_startIndex(whole, fromidx));
        } catch (Exception e) {
//            CrashHandler.errorDialog(e);
//            e.printStackTrace();
            return null;
        }
//        return afterprt;
        return p.trim();
    }


    public static Prt cat(String pre, String after, int fromidx, String whole) {
        Prt afterprt = null;
        String p = null;
        int start = 0;
        try {
            start = new Prt(pre).getAfterIndex(whole, fromidx);
            if (start < 0) {
                return null;
            }
            p = whole.substring(start, (afterprt = new Prt(after)).getPreviousIndex(whole, start));
        } catch (Exception e) {
//            CrashHandler.errorDialog(e);
//            e.printStackTrace();
            return null;
        }
//        return afterprt;
        return new Prt(p).setBoundPre(pre, whole).setBoundAfter(after, start + p.length(), whole);
    }

    public String getPrt() {
        return prt;
    }

    public int preidx;
    public int afteridx;

    public Prt setBoundPre(String prestr, String txt) {
        Prt prt = new Prt(prestr);

        boundpreidx = prt.getAfterIndex(txt);
        return this;
    }

    public int boundpreidx;
    public int boundafteridx;

    public Prt setBoundAfter(String prestr, int fromindex, String txt) {
        Prt prt = new Prt(prestr);

        boundafteridx = prt.getPreviousIndex(txt, fromindex);
        return this;
    }

    public Prt setBoundAfter(String prestr, String txt) {
        Prt prt = new Prt(prestr);

        boundafteridx = prt.getPreviousIndex(txt);
        return this;
    }

    public int getBoundpreidx() {
        return boundpreidx;
    }

    public int getBoundafteridx() {
        return boundafteridx;
    }

    public int getPreidx() {
        return preidx;
    }

    public int getAfteridx() {
        return afteridx;
    }
}